package cn.biplam.front.page;

import java.io.IOException;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.Operator;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.opcode.SysManageCode.UserOptCode;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.common.wrapper.sqlresult.SqlResult;

/**
 * 用户登陆处理类
 * @author 	WangSong 2009-6-10 下午03:27:20
 * @version 1.0
 */
public class LoginDo{
	private HttpServletRequest request=null;
	private HttpServletResponse response=null;

	/**
	 * 功能：构造方法
	 * @param request
	 * @param response
	 */
	public LoginDo(HttpServletRequest request, HttpServletResponse response){
		this.request=request;
		this.response=response;
	}

	/**
	 * 返回页面的HTML
	 * @throws ErrorMessage 
	 */
	public void draw() throws ErrorMessage, IOException{
		String sHomeUrl = request.getContextPath();

		String act=request.getParameter("act");
		if(act!=null){//退出处理
			Cookie[] cks = request.getCookies();
			if(cks!=null){
				Cookie ck=null;
				for(Cookie ite:cks){
					if(ite.getName().equals("userinfo")){
						ck=ite;
					}
				}
				if(ck!=null){
					ck.setMaxAge(0);
					response.addCookie(ck);
				}
			}
			try {
				response.sendRedirect(sHomeUrl+"/Login.bi");
			} catch (IOException e) {
				throw new ErrorMessage(e,"Cookie redirect failed!");
			}
			return;
		}

		String userName = request.getParameter("userName");
		String userPassword = request.getParameter("userPassword");
		try{
			this.checkUser(userName,userPassword,request,response);
			response.sendRedirect(sHomeUrl+"/Main.bi");
		} catch (ErrorMessage e) {
			throw e;
		} catch (Exception e) {
			throw new ErrorMessage(e);
		}
	}

	/**
	 * 判断用户是否已经登陆，如果没有登陆跳转到登陆页面。
	 * @param request 
	 * @param response
	 * @param mUser
	 * @return	UserInfo
	 * @throws ErrorMessage
	 */
	public static UserInfo isLogin(HttpServletRequest request,HttpServletResponse response) throws ErrorMessage{
		UserInfo mUser = null; 
		try {
			mUser = (UserInfo)request.getSession().getAttribute("userInfo");
			if(mUser==null){
				response.sendRedirect(request.getContextPath()+"/Login.bi");
			}
		}catch (Exception e) {
			throw new ErrorMessage(e);
		} 
		return mUser;
	}

	/**
	 * 验证用户
	 * @param userName  用户名
	 * @param userPassword  密码
	 * @return boolean true用户登陆成功  false登陆失败
	 * @throws ErrorMessage
	 */
	private void checkUser(String userName,String userPassword,
			HttpServletRequest request, HttpServletResponse response) throws ErrorMessage{
		DataFilter filter=new DataFilter();
		filter.put("usercode", userName);
		OperatorResult or=Operator.get(UserOptCode.Query_By_User_Code,userName,filter);
		SqlResult sr = or.getSelectResult();
		if(sr.size()>0){
			String password = sr.get(0,"password");
			if(password!=null && password.equals(userPassword)){
				/**设置Session*/
				this.setSystemSession(request,response,sr);
			}else{
				throw new ErrorMessage("用户名或密码错误!");
			}
		}else{
			throw new ErrorMessage("无法获得用户验证数据!");
		}
	}

	/**
	 * 设置Session信息信息
	 * @param request
	 * @param response
	 * @param sr  用户信息
	 * @throws ErrorMessage
	 */
	private void setSystemSession(HttpServletRequest request, 
			HttpServletResponse response,SqlResult sr) throws ErrorMessage{
		UserInfo muserInfo = new UserInfo();
		muserInfo.setUserID(sr.get(0,"USERID"));
		muserInfo.setUserCallName(sr.get(0,"USERNAME"));
		muserInfo.setUserLanguage(sr.get(0,"userlanguage"));
		request.getSession().setAttribute("userInfo",muserInfo);
		request.getSession().setMaxInactiveInterval(360000);

		Cookie ck = new Cookie("userinfo",sr.get(0,"usercode")+","+sr.get(0,"password"));
		ck.setMaxAge(365*24*60*60);
		response.addCookie(ck);
	}
}
